package xyc.sjxy.exam.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import sjxy.reflect.ReflectionUtils;
import xyc.sjxy.exam.dao.DbDao;
import xyc.sjxy.exam.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * 这是一个征对所有表的增删改查的通用类
 * @param <T>
 */
public class DbDaoImpl<T> implements DbDao<T> {
    private QueryRunner qr=null;
    private Class<T> type=null;
    public DbDaoImpl(){
        this.qr=new QueryRunner(JdbcUtils.getDataSource());
        this.type= ReflectionUtils.getSuperGenericType(getClass());
    }
    @Override
    public T find(String sql, Object... args) throws SQLException {
        return qr.query(sql,new BeanHandler<>(type),args);
    }

    @Override
    public List<T> findAll(String sql, Object... args) throws SQLException {
        return qr.query(sql,new BeanListHandler<>(type),args);
    }

    @Override
    public int update(String sql, Object... args) throws SQLException {
        return qr.update(sql,args);
    }
}
